﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace IceDog.Lib.Linq
{
    public class LinqPrac
    {
        #region virtual data
        private class Employee
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public decimal Salary { get; set; }
            public DateTime StartDate { get; set; }

            public override string ToString()
            {
                return this.FirstName + "." + this.LastName;
            }
        }
        private string[] nameList = { "ads", "dgh", "goh", "gwp", "wsh", "ahr", "ehw", "sdg", "wer", "gwh", "fag" };
        List<Employee> employeeList = new List<Employee>()
            {
                new Employee()
                {
                    FirstName = "Tom",
                    LastName = "Park",
                    StartDate = DateTime.Parse("1/5/1999"),
                    Salary = 8000
                },
                new Employee()
                {
                    FirstName = "Aton",
                    LastName = "Park",
                    StartDate = DateTime.Parse("1/5/1979"),
                    Salary = 10000
                },
                new Employee()
                {
                    FirstName = "Jim",
                    LastName = "Park",
                    StartDate = DateTime.Parse("1/5/1989"),
                    Salary = 9999
                },
                new Employee()
                {
                    FirstName = "Black",
                    LastName = "Park",
                    StartDate = DateTime.Parse("1/5/1969"),
                    Salary = 7589
                },
            };
        private int[] GenerateNumList(int num)
        {
            var ran = new Random();
            int[] arr = new int[num];
            while (!(num--.Equals(0)))
            {
                arr[num] = ran.Next(1, 99999);
            }
            return arr;
        }
        #endregion
        #region practice
        public void prac1()
        {
            var res1 = nameList.Where(delegate (string item)
            {
                return item.StartsWith("a");
            });
            var res2 = nameList.Where(item => item.StartsWith("a"));
            var res3 = from name in nameList where name.StartsWith("a") select name;
            Console.WriteLine();
        }
        public void prac2()
        {
            var res1 = nameList.Where<string>(name => name.Contains('w')).OrderByDescending(n => n);
            var res2 = nameList.Where(delegate (string name)
            {
                return name.Contains('w');
            }).OrderByDescending(delegate (string name) { return name; });
            var res3 = from name in nameList where name.Contains('w') orderby name descending select name;
            Console.WriteLine();
        }
        public void prac3()
        {
            var arr = GenerateNumList(100);
            var res1 = arr.Where(num => num < 1000);
            var res2 = from num in arr where num < 1000 select num;
            Console.WriteLine();
        }
        public void prac4()
        {
            var arr = GenerateNumList(100);
            var count1 = arr.Count();
            var max1 = arr.Max();
            var min1 = arr.Min();
            var avg1 = arr.Average();
            Console.WriteLine();
        }
        public void prac5()
        {
            var res1 = from employee in employeeList
                       where employee.Salary > 7500
                       orderby employee.LastName ascending
                       select employee;
            var res2 = from employee in employeeList
                       where employee.Salary > 7500
                       orderby employee.LastName ascending
                       select new
                       {
                           FirstName = employee.FirstName,
                           LastName = employee.LastName
                       };
            var res3 = from employee in employeeList
                       where employee.Salary > 7500
                       orderby employee.LastName ascending
                       select employee.FirstName;
            Console.WriteLine();
        }
        #endregion
    }
}
